   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 
   
OAA_CI.controls = OAA_CI.controls || {}; 

/** ================================================================
*
* Controls Module initialization
*
* =============================================================== */

OAA_CI.control_elements = [];


/** ================================================================
*
* onLoad
*
* @desc Initializes forms module
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.controls_nls_strings = document.getElementById("ID_STRINGBUNDLE_CONTROLS");        

//  OpenAjax.a11y.console("Loading controls strings: " + OAA_CI.controls_nls_strings);
  
  OAA_CI.controls.onShowAllControlsCommand();
  
}; 

/** ================================================================
*
* onShowLabelsCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with a list of just label/legend information
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onShowLabelsCommand = function(e) {

  var xul_treeitem;
  var i;
  var le;

  OAA_CI.evaluateDocument();   
  
  var label_elements = OAA_CI.evaluation.dom_cache.controls_cache.label_elements;
  
  if (label_elements === null) return;
  
//  OpenAjax.a11y.console("Labels: " + label_elements.length); 

  var label_elements_len = label_elements.length;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_LABELS');
  

  OAA_CI.removeChildNodes(xul_treechildren);

  if (label_elements_len) {
  
    for (i=0; i<label_elements_len; i++) {
    
      le = label_elements[i];
    
      xul_treeitem = OAA_CI.createXULElement('treeitem', []);

      OAA_CI.addTreeRow(xul_treeitem, [le.dom_element.tag_name, le.type, le.label], le.cache_id );   
      
      xul_treechildren.appendChild(xul_treeitem);
      
    } // loop
    
  }
  else {
    xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    

};  

/** ================================================================
*
* onShowControlsCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with list of just control information
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onShowControlsCommand = function(e) {

  var xul_treeitem;
  var i;
  var ce;

  OAA_CI.evaluateDocument();   
  
  var control_elements = OAA_CI.evaluation.dom_cache.controls_cache.control_elements;
  
  if (control_elements === null) return;
  
//  OpenAjax.a11y.console("Controls: " + control_elements.length); 

  var control_elements_len = control_elements.length;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_CONTROLS');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  if (control_elements_len) {
  
    for (i=0; i<control_elements_len; i++) {
    
      ce = control_elements[i];
    
      xul_treeitem = OAA_CI.createXULElement('treeitem', []);

      OAA_CI.addTreeRow(xul_treeitem, [ce.dom_element.tag_name, ce.type, ce.label], ce.cache_id );   
      
      xul_treechildren.appendChild(xul_treeitem);
      
    } // loop
    
  }
  else {
    xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    
};  


/** ================================================================
*
* onShowAllControlsCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with form information
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onShowAllControlsCommand = function(e) {

  OAA_CI.evaluateDocument();   
  
  var child_controls = OAA_CI.evaluation.dom_cache.controls_cache.child_controls;
  
//  OpenAjax.a11y.console("All Controls: " + child_controls.length); 

  if (child_controls === null) return;
  
  var child_controls_len = child_controls.length;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_ALL_CONTROLS');

  OAA_CI.removeChildNodes(xul_treechildren);
  OAA_CI.control_elements = [];

  if (child_controls_len) {
    addFormControlChildren(xul_treechildren, child_controls);
  }
  else {
    xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    

  function addFormControlChildren(xul_treechildren, control_elements) {
  
    var label;
    var ce;
    var control_elements_len = control_elements.length;
    
    for (var i=0; i<control_elements_len; i++) {
    
      ce = control_elements[i];
    
      var xul_treeitem = OAA_CI.createXULElement('treeitem', []);

      OAA_CI.addTreeRow(xul_treeitem, [ce.dom_element.tag_name, ce.type, ce.label], ce.cache_id );
            
      if (ce.child_controls && ce.child_controls.length) {
      
         xul_treeitem.setAttribute("container", "true");
         xul_treeitem.setAttribute("open", "true");

         var xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);

         addFormControlChildren(xul_sub_treechildren, ce.child_controls);
         
         xul_treeitem.appendChild(xul_sub_treechildren);
         
      }
      
      xul_treechildren.appendChild(xul_treeitem);
      
    } // loop
  }   
};  

/** ================================================================
*
* onLabelElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the list of label elements
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onLabelElementItemsSelectChanged = function (event) {

  var le;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var controls_cache = OAA_CI.evaluation.dom_cache.controls_cache;  
  var xul_tree = document.getElementById('ID_TREE_LABELS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Control cache id: " + cache_id );

    le = controls_cache.getLabelElementByCacheId(cache_id);
    
    if (le) {
      OAA_CI.showCacheItemProperties(le);
    }
  } 

};

/** ================================================================
*
* onControlElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the form elements
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onControlElementItemsSelectChanged = function (event) {

  var ce;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var controls_cache = OAA_CI.evaluation.dom_cache.controls_cache;  
  var xul_tree = document.getElementById('ID_TREE_CONTROLS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Control cache id: " + cache_id );

    ce = controls_cache.getControlElementByCacheId(cache_id);
    
    if (ce) {
      OAA_CI.showCacheItemProperties(ce);
    }
  } 
  
};


/** ================================================================
*
* onAllControlElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the form elements
*
* @return nothing
*
* =============================================================== */

OAA_CI.controls.onAllControlElementItemsSelectChanged = function (event) {


  var ce;
  var le;
  var fe;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var controls_cache = OAA_CI.evaluation.dom_cache.controls_cache;  
  var xul_tree = document.getElementById('ID_TREE_ALL_CONTROLS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("All control cache id: " + cache_id );

    ce = controls_cache.getControlElementByCacheId(cache_id);
    
    if (ce) {
      OAA_CI.showCacheItemProperties(ce);
    }
    else {    
      le = controls_cache.getLabelElementByCacheId(cache_id);
      if (le) {
       OAA_CI.showCacheItemProperties(le);
      } 
      else {
        fe = controls_cache.getFormElementByCacheId(cache_id);
        if (fe) {
          OAA_CI.showCacheItemProperties(fe);     
        } 
        else {
          fe = controls_cache.getFieldsetElementByCacheId(cache_id);
          if (fe) {
            OAA_CI.showCacheItemProperties(fe);             
          }
        }
      }      
    }
  } 
  
};
